package chess_game_5;

import java.util.Arrays;

//单个棋盘下棋
public class ChessBoard {
    private final char[][] board; // 8x8棋盘数组
    private final Player player; // 关联的玩家

    // 初始化棋盘和玩家
    public ChessBoard(String player1Name, String player2Name) {
        this.board = new char[Chess.BOARD_SIZE][Chess.BOARD_SIZE];
        this.player = new Player(player1Name, player2Name);
        initializeBoard();
    }

    // 重置棋盘为空状态
    private void initializeBoard() {
        for (char[] row : board) {
            Arrays.fill(row, Chess.EMPTY);
        }
    }

    // 尝试落子（成功返回true）
    public boolean placeChess(int row, int col) {
        // 检查位置有效性
        if (row < 0 || row >= Chess.BOARD_SIZE ||
                col < 0 || col >= Chess.BOARD_SIZE ||
                board[row][col] != Chess.EMPTY) {
            return false;
        }

        // 放置当前玩家的棋子
        board[row][col] = player.isCurrentPlayer1() ? Chess.PLAYER1_SYMBOL : Chess.PLAYER2_SYMBOL;
        player.switchPlayer(); // 切换回合
        return true;
    }

    // 检查棋盘是否已满
    public boolean isFull() {
        for (char[] row : board) {
            for (char cell : row) {
                if (cell == Chess.EMPTY)
                    return false;
            }
        }
        return true;
    }

    // 获取当前棋盘状态（只读）
    public char[][] getBoard() {
        return board;
    }

    // 获取关联的玩家对象
    public Player getPlayer() {
        return player;
    }
}